import { state } from "./state";
import type { State } from "./state";
import { InjectionKey } from "vue";
import {
  Store as VuexStore,
  CommitOptions,
  DispatchOptions,
  Module,
} from "vuex";
import { Getters, getters } from "./getters";
import { Mutations, Mutation, mutations } from "./mutations";
import { Actions, actions } from "./actions";
import { RootState } from "..";

//key值  混入必须参数
export const key: InjectionKey<VuexStore<State>> = Symbol();

//定义局部module的store类型
export type Store<S = State> = Omit<
  VuexStore<S>,
  "getters" | "commit" | "dispatch"
> & {
  getters: {
    [K in keyof Getters]: ReturnType<Getters[K]>;
  };
} & {
  commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
    key: K,
    palyload: P,
    options?: CommitOptions
  ): ReturnType<Mutations[K]>;
} & {
  dispatch<K extends keyof Actions>(
    key: K,
    palyload?: Parameters<Actions[K]>[1],
    options?: DispatchOptions
  ): ReturnType<Actions[K]>;
};

//单个局部的module的store设置
//局部状态state 根节点状态则为rootState
export const store: Module<State, RootState> = {
  state,
  mutations,
  actions,
  getters,
};

export { State, Mutation };
